package org.ybxb.okhttp.interceptor;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @ProjectName: generator-report
 * @PackageName: org.ybxb.gr
 * @Description:
 * @Author: hy.bai
 * @Date: 2025-07-08 16:27
 */
public class LoggingInterceptor implements Interceptor {

  private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);

  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    // 记录请求信息
    long startTime = System.nanoTime();
    logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));
    Response response = chain.proceed(request);
    long endTime = System.nanoTime();
    logger.info(String.format("Received response for %s in %.1fms%n%s", response.request().url(),
        (endTime - startTime) / 1e6d, response.headers()));
    return response;
  }
}
